package com.jeesite.modules.dp.service.yqrecord;

import java.util.List;

import com.jeesite.modules.enums.DpRecordTypeEnum;
import com.jeesite.modules.enums.OrderTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.dp.dao.yqrecord.DpYqrecordDao;
import com.jeesite.modules.dp.entity.kecheng.DpKecheng;
import com.jeesite.modules.dp.entity.yqrecord.DpYqrecord;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.EmpUserService;
import com.jeesite.modules.sys.service.UserService;

/**
 * 邀请记录Service
 *
 * @author hancing.com
 * @version 2024-12-16
 */
@Service
public class DpYqrecordService extends CrudService<DpYqrecordDao, DpYqrecord> {

    @Autowired
    private UserService userService;
    @Autowired
    private EmpUserService empUserService;

    /**
     * 获取单条数据
     *
     * @param dpYqrecord
     * @return
     */
    @Override
    public DpYqrecord get(DpYqrecord dpYqrecord) {
        return super.get(dpYqrecord);
    }

    /**
     * 查询分页数据
     *
     * @param dpYqrecord 查询条件
     * @param dpYqrecord page 分页对象
     * @return
     */
    @Override
    public Page<DpYqrecord> findPage(DpYqrecord dpYqrecord) {
        return super.findPage(dpYqrecord);
    }

    public Page<DpYqrecord> frontfindPage(Page<DpYqrecord> page, String loginCode, String type) {
        DpYqrecord dpYqrecord = new DpYqrecord();
        dpYqrecord.setPage(page);
        // 设置分页参数，传递给MyEntityDao的查询方法
        if ("0".equals(type)) {
            dpYqrecord.setYqr(loginCode);
            page.setList(dao.frontfindPageRen(dpYqrecord));
        } else {
            dpYqrecord.setYqr(loginCode);
            page.setList(dao.frontfindPageMoney(dpYqrecord));
        }
        return page;
    }

    /**
     * 查询列表数据
     *
     * @param dpYqrecord
     * @return
     */
    @Override
    public List<DpYqrecord> findList(DpYqrecord dpYqrecord) {
        return super.findList(dpYqrecord);
    }

    /**
     * 保存数据（插入或更新）
     *
     * @param dpYqrecord
     */
    @Override
    @Transactional
    public void save(DpYqrecord dpYqrecord) {
        super.save(dpYqrecord);
    }

    /**
     * 更新状态
     *
     * @param dpYqrecord
     */
    @Override
    @Transactional
    public void updateStatus(DpYqrecord dpYqrecord) {
        super.updateStatus(dpYqrecord);
    }

    /**
     * 删除数据
     *
     * @param dpYqrecord
     */
    @Override
    @Transactional
    public void delete(DpYqrecord dpYqrecord) {
        super.delete(dpYqrecord);
    }

    @Transactional
    public User getUserByYqm(String yqm) {
        return dao.getUserByYqm(yqm);
    }

    @Transactional
    public void updateUserMoney(String logincode, double money) {
        dao.updateUserMoney(logincode, money);
    }

    /**
     * 保存邀请记录
     *
     * @param eu     注册人
     * @param yqUser 邀请码对应用户
     */
    @Transactional
    public void saveRegUser(EmpUser eu, User yqUser) {
        // 记录邀请
        if (yqUser != null) {
            DpYqrecord record = new DpYqrecord();
            record.setIsNewRecord(true);
            record.setYqr(yqUser.getLoginCode());
            record.setYqrname(yqUser.getUserName());
            record.setByqr(eu.getLoginCode());
            record.setByqrname(eu.getUserName());
            record.setQyjtype(DpRecordTypeEnum.USER_REGISTER.getType());
            record.setQyjtitle("邀请用户注册");

            save(record);
        }
    }

    @Transactional
    public DpYqrecord getUserTjr(String byqr) {
        return dao.getUserTjr(byqr);
    }

    @Transactional
    public void addOrderFanxian(String tjr, String byqr, Double addQyj, String orderType) {
        OrderTypeEnum orm = OrderTypeEnum.getOfType(orderType);
        String fanxianDesc = orm == null ? "" : orm.getName();
        DpYqrecord dpYqrecord1 = new DpYqrecord();
        dpYqrecord1.setIsNewRecord(true);
        dpYqrecord1.setYqr(tjr);

        dpYqrecord1.setQyj(addQyj);
        dpYqrecord1.setQyjtype(DpRecordTypeEnum.YQ_FANXIAN.getType());
        dpYqrecord1.setQyjtitle(fanxianDesc);

        save(dpYqrecord1);

        DpYqrecord dpYqrecord2 = new DpYqrecord();
        dpYqrecord2.setIsNewRecord(true);
        dpYqrecord2.setYqr(byqr);
        dpYqrecord2.setQyjtype(DpRecordTypeEnum.YQ_FANXIAN.getType());

        dpYqrecord2.setQyj(addQyj);
        dpYqrecord2.setQyjtitle(fanxianDesc);

        save(dpYqrecord2);

        // 更新用户金额
        updateUserMoney(tjr, addQyj);
        updateUserMoney(byqr, addQyj);
    }
}